package com.example.module.custom.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;


import com.example.module.custom.R;
import com.example.module.custom.R2;
import com.example.module.custom.bean.PQCDetailsBean;
import com.example.module.custom.bean.PollingBean;
import com.example.module.custom.fragment.PQCTestFragment;
import com.example.module.custom.ui.PQCAddActivity;
import com.example.zhouwei.library.CustomPopWindow;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.nand.library.base.utils.DialogLookShow;

import org.angmarch.views.NiceSpinner;
import org.angmarch.views.OnSpinnerItemSelectedListener;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;

public class PatrolPageAdapter extends BaseAdapter<PQCDetailsBean.DetailListBean> implements View.OnClickListener {
    private Context mContext;
    private int layoutId;
    private int flag;//功能选项（1、新增pqc，2、复查pqc 3、查看详情【不可操作】）
    //private List<PQCDetailsBean.DetailListBean> mList = new ArrayList<>();
    ViewHolder holder1;
   // private boolean isQualified;
    private String TAG = "PatrolPageAdapter";

    private OnItemClickListener mOnItemClickListener = null;
    private String cont = "";
    private boolean isClear;//是否清空


    //public boolean isQualified() {
      //  return isQualified;
    //}

    public PatrolPageAdapter(Context mContext, List<PQCDetailsBean.DetailListBean> data, int layoutId, int flag) {
        super(mContext, data, layoutId);
        this.flag = flag;
        this.layoutId = layoutId;
        this.mContext = mContext;
    }

    public void setData(List<PQCDetailsBean.DetailListBean> data) {
        Log.i("PatrolPageAdapter", "initView: -----pqcDetailsBeans----" + data.size());
        this.tlist = data;
        notifyDataSetChanged();
    }
    /**
     *  @author ljp
     *  @time   14:31
     *  @describe 清空数据信息，恢复默认
     */
    public void clearEdit(){
        this.tlist.clear();
        isClear = true;
        notifyDataSetChanged();

    }

    public List<PQCDetailsBean.DetailListBean> getPqcList() {
        return tlist;
    }


    /*@Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View layout = LayoutInflater.from(viewGroup.getContext()).inflate(layoutId, viewGroup, false);
        layout.setOnClickListener(this);
        return new ViewHolder(layout);
    }*/

    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof ViewHolder) {
            holder1 = (ViewHolder) viewHolder;
            ((ViewHolder) viewHolder).edittext.setVisibility(View.GONE);
            final ViewHolder holder = (ViewHolder) viewHolder;
            final PQCDetailsBean.DetailListBean detailListBean = tlist.get(position);
            Log.i(TAG, "onBindViewHolder: -------detailListBean----"+detailListBean.toString());
            if(isClear)
                holder.edittext.setText("");

            Log.i(TAG, "onBindViewHolder: -------flag----"+flag);
            String accContent = detailListBean.getCheckItem();
            final String standContent = detailListBean.getCheckContent();
            holder.tvItem.setText(accContent);
            holder.tvContent.setText(standContent);
            holder.etValue.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            //点击查看检查内容详情
            holder.tvContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   // DialogLookShow.showDialog(context,"检验内容",standContent);
                   /* new XPopup.Builder(context)
                            .asConfirm("检验内容", standContent,
                            new OnConfirmListener() {
                                @Override
                                public void onConfirm() {

                                }
                            })
                            .show();*/
                }
            });
            switch (flag) {
                case 1:
                    //isQualified = true;//默认全部合格
                    holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup radioGroup, int i) {
                            if(i == holder.radioButton1.getId()){
                                holder.tvTitle.setVisibility(View.GONE);
                                holder.viewLine.setVisibility(View.GONE);
                                holder.edittext.setVisibility(View.GONE);
                                OrQu = 0;
                                detailListBean.setResult(OrQu);
                                detailListBean.setUnqualiFiedDescribe("");
                            }else {
                                holder.tvTitle.setVisibility(View.VISIBLE);
                                holder.viewLine.setVisibility(View.VISIBLE);
                                holder.edittext.setVisibility(View.VISIBLE);
                                OrQu = 1;
                                detailListBean.setResult(OrQu);
                            }

                        }
                    });
                   // holder.edittext.setVisibility(View.GONE);
                    //OrQu = 1;
                    //detailListBean.setResult(OrQu);
                    holder.edittext.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before,
                                                  int count) {
                            cont = s.toString();
                            // mList.get(position).setReasonNG(cont);
                        }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            String trim = s.toString().trim();
                            if (trim.length() > 0)
                                detailListBean.setUnqualiFiedDescribe(trim);
                        }
                    });

                    holder.etValue.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void afterTextChanged(Editable editable) {
                            String trim = editable.toString().trim();
                            if (trim.length() > 0)
                                detailListBean.setCheckValue(Double.valueOf(trim));
                        }
                    });
                    break;
                case 2:
                        String describe = detailListBean.getUnqualiFiedDescribe();
                        if (detailListBean.getResult() == 1) {
                            if (describe.isEmpty() || describe == null) {
                                holder.tvTitle.setVisibility(View.GONE);
                                holder.edittext.setVisibility(View.GONE);
                                holder.viewLine.setVisibility(View.GONE);
                            } else {
                                holder.tvTitle.setVisibility(View.VISIBLE);
                                holder.viewLine.setVisibility(View.VISIBLE);
                                holder.edittext.setVisibility(View.VISIBLE);
                                holder.edittext.setText(describe);
                                holder.edittext.setSelection(describe.length());//将光标移至文字末尾
                            }
                        }
                        holder.etValue.setText(detailListBean.getCheckValue() + "");
                        holder.etValue.setSelection(holder.etValue.getText().length());
                        if (0 == detailListBean.getResult()){
                            holder.radioButton1.setChecked(true);
                        }else {
                            holder.radioButton2.setChecked(true);
                        }
                    holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup radioGroup, int i) {
                            if(i == holder.radioButton1.getId()){
                                holder.tvTitle.setVisibility(View.GONE);
                                holder.viewLine.setVisibility(View.GONE);
                                holder.edittext.setVisibility(View.GONE);
                                OrQu = 0;
                                detailListBean.setResult(OrQu);
                                detailListBean.setUnqualiFiedDescribe("");
                            }else {
                                holder.tvTitle.setVisibility(View.VISIBLE);
                                holder.viewLine.setVisibility(View.VISIBLE);
                                holder.edittext.setVisibility(View.VISIBLE);
                                holder.edittext.setSelection(holder.edittext.getText().toString().length());//将光标移至文字末尾
                                OrQu = 1;
                                detailListBean.setResult(OrQu);
                            }
                        }
                    });
                        holder.edittext.addTextChangedListener(new TextWatcher() {
                            @Override
                            public void onTextChanged(CharSequence s, int start, int before,
                                                      int count) {
                                cont = s.toString();
                                // mList.get(position).setReasonNG(cont);
                            }

                            @Override
                            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                            }

                            @Override
                            public void afterTextChanged(Editable s) {
                                String trim = s.toString().trim();
                                if (trim.length() > 0)
                                    detailListBean.setUnqualiFiedDescribe(trim);
                            }
                        });

                        holder.etValue.addTextChangedListener(new TextWatcher() {
                            @Override
                            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                            }

                            @Override
                            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                            }

                            @Override
                            public void afterTextChanged(Editable editable) {
                                String trim = editable.toString().trim();
                                if (trim.length() > 0)
                                    detailListBean.setCheckValue(Double.valueOf(trim));

                            }
                        });
                    break;
                case 3:
                    holder.etValue.setText(detailListBean.getCheckValue() + "");
                    int result = detailListBean.getResult();
                    //holder.spinnerChoose.setVisibility(View.GONE);
                   // holder.textViewChoose.setVisibility(View.VISIBLE);
                   // holder.textViewChoose.setText(detailListBean.getResult() == 0 ? "合格" : "不合格");
                    for (int i = 0; i < holder.radioGroup.getChildCount(); i++) {
                        if (0 == result){
                            holder.radioButton1.setChecked(true);
                        }else {
                            holder.radioButton2.setChecked(true);
                        }
                        holder.radioGroup.getChildAt(i).setEnabled(false);
                    }
                    holder.etValue.setFocusable(false);
                    holder.etValue.setFocusableInTouchMode(false);
                    holder.etValue.setCursorVisible(false);
                    String reasonNG = detailListBean.getUnqualiFiedDescribe();
                    if (detailListBean.getResult() == 1)
                        if (reasonNG.isEmpty() || reasonNG == null) {
                            holder.tvTitle.setVisibility(View.GONE);
                            holder.textview.setVisibility(View.GONE);
                            holder.viewLine.setVisibility(View.GONE);
                        } else {
                            holder.tvTitle.setVisibility(View.VISIBLE);
                            holder.viewLine.setVisibility(View.VISIBLE);
                            holder.textview.setVisibility(View.VISIBLE);
                            holder.textview.setText(reasonNG);
                        }
                    break;
            }
        }

    }

    private int OrQu = 1;



    class ViewHolder extends BaseViewHolder {
        @BindView(R2.id.tv_item)
        TextView tvItem;
        @BindView(R2.id.tv_content)
        TextView tvContent;
        @BindView(R2.id.et_value)
        EditText etValue;
         @BindView(R2.id.radio_btn_eligi)
         RadioButton radioButton1;
         @BindView(R2.id.radio_btn_uneligi)
         RadioButton radioButton2;
         @BindView(R2.id.radio_group)
         RadioGroup radioGroup;
        @BindView(R2.id.edittext)
        EditText edittext;
        @BindView(R2.id.textview)
        TextView textview;
        @BindView(R2.id.view_line)
        View viewLine;
        @BindView(R2.id.tv_title_edittext)
        TextView tvTitle;



        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onBhgClick(int position, String context);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}


